﻿package morn.core.components
{
    import flash.display.*;
    import flash.events.*;
    import morn.core.handlers.*;
    import morn.core.utils.*;

    public class Button extends Component implements ISelect, ISkinAbleContainer
    {
        protected var _customState:Object;
        protected var _bitmap:AutoBitmap;
        protected var _btnLabel:Label;
        private var _skinChanged:Boolean = false;
        protected var _clickHandler:Handler;
        protected var _labelColors:Array;
        protected var _labelMargin:Array;
        protected var _state:int;
        protected var _toggle:Boolean;
        protected var _selected:Boolean;
        protected var _skin:String;
        protected var _skinbitmapdata:BitmapData;
        protected var _autoSize:Boolean = true;
        static var stateMap:Object = {rollOver:1, rollOut:0, mouseDown:2, mouseUp:1, selected:2};

        public function Button(param1:String = null, param2:String = "", param3:String = null)
        {
            this._labelColors = Styles.buttonLabelColors;
            this._labelMargin = Styles.buttonLabelMargin;
            this.skin = param1;
            this.label = param2;
            this.buttonMode = true;
            if (param3 != null)
            {
                this.customState = param3;
            }
            return;
        }// end function

        public function setSkin(param1:Object) : void
        {
            if (!this._skinChanged)
            {
                this.skin = param1 as String;
            }
            return;
        }// end function

        public function set skinChanged(param1:Boolean) : void
        {
            this._skinChanged = param1;
            return;
        }// end function

        override protected function createChildren() : void
        {
            var _loc_1:* = new AutoBitmap();
            this._bitmap = new AutoBitmap();
            addChild(_loc_1);
            var _loc_1:* = new Label();
            this._btnLabel = new Label();
            addChild(_loc_1);
            return;
        }// end function

        override protected function initialize() : void
        {
            this._btnLabel.align = "center";
            addEventListener(MouseEvent.ROLL_OVER, this.onMouse);
            addEventListener(MouseEvent.ROLL_OUT, this.onMouse);
            addEventListener(MouseEvent.MOUSE_DOWN, this.onMouse);
            addEventListener(MouseEvent.MOUSE_UP, this.onMouse);
            addEventListener(MouseEvent.CLICK, this.onMouse);
            return;
        }// end function

        protected function onMouse(event:MouseEvent) : void
        {
            if (this._toggle == false && this._selected || _disabled)
            {
                return;
            }
            if (event.type == MouseEvent.CLICK)
            {
                if (this._toggle)
                {
                    this.selected = !this._selected;
                }
                if (this._clickHandler)
                {
                    this._clickHandler.execute();
                }
                sendEvent(Event.SELECT);
                return;
            }
            if (this._selected == false)
            {
                this.state = this._customState ? (this._customState[event.type]) : (stateMap[event.type]);
            }
            return;
        }// end function

        public function get label() : String
        {
            return this._btnLabel.text;
        }// end function

        public function set label(param1:String) : void
        {
            if (this._btnLabel.text != param1)
            {
                this._btnLabel.text = param1;
                callLater(this.changeState);
            }
            return;
        }// end function

        public function get skin() : String
        {
            return this._skin;
        }// end function

        public function set skin(param1:String) : void
        {
            if (this._skin != param1)
            {
                this._skin = param1;
                this._bitmap.clips = App.asset.getClips(this._skin, 1, 3);
                if (this._bitmap.clips && this._bitmap.clips[0])
                {
                    this._skinbitmapdata = this._bitmap.clips[0];
                }
                if (this._autoSize)
                {
                    _contentWidth = this._bitmap.width;
                    _contentHeight = this._bitmap.height;
                }
                callLater(this.changeLabelSize);
                this._bitmap.url = param1;
            }
            this._skinChanged = true;
            return;
        }// end function

        public function get skinbitmap() : BitmapData
        {
            return this._skinbitmapdata;
        }// end function

        protected function changeLabelSize() : void
        {
            this._btnLabel.width = width - this._labelMargin[0] - this._labelMargin[2];
            this._btnLabel.height = ObjectUtils.getTextField(this._btnLabel.format).height;
            this._btnLabel.x = this._labelMargin[0];
            this._btnLabel.y = (height - this._btnLabel.height) * 0.5 + this._labelMargin[1] - this._labelMargin[3];
            return;
        }// end function

        public function get selected() : Boolean
        {
            return this._selected;
        }// end function

        public function set selected(param1:Boolean) : void
        {
            if (this._selected != param1)
            {
                this._selected = param1;
                this.state = this._customState ? (this._selected ? (this._customState["selected"]) : (this._customState["rollOut"])) : (this._selected ? (stateMap["selected"]) : (stateMap["rollOut"]));
            }
            return;
        }// end function

        protected function get state() : int
        {
            return this._state;
        }// end function

        protected function set state(param1:int) : void
        {
            this._state = param1;
            this.changeState();
            return;
        }// end function

        protected function changeState() : void
        {
            this._bitmap.index = this._state;
            this._btnLabel.color = this._labelColors[this._state];
            return;
        }// end function

        public function get toggle() : Boolean
        {
            return this._toggle;
        }// end function

        public function set toggle(param1:Boolean) : void
        {
            this._toggle = param1;
            return;
        }// end function

        override public function set disabled(param1:Boolean) : void
        {
            if (_disabled != param1)
            {
                super.disabled = param1;
                this.state = this._customState ? (this._selected ? (this._customState["selected"]) : (this._customState["rollOut"])) : (this._selected ? (stateMap["selected"]) : (stateMap["rollOut"]));
                ObjectUtils.gray(this, _disabled);
            }
            return;
        }// end function

        public function get labelFont() : String
        {
            return this._btnLabel.font;
        }// end function

        public function set labelFont(param1:String) : void
        {
            this._btnLabel.font = param1;
            callLater(this.changeLabelSize);
            return;
        }// end function

        public function get labelColors() : String
        {
            return String(this._labelColors);
        }// end function

        public function set labelColors(param1:String) : void
        {
            this._labelColors = StringUtils.fillArrayByString(this._labelColors, param1);
            callLater(this.changeState);
            return;
        }// end function

        public function get labelMargin() : String
        {
            return String(this._labelMargin);
        }// end function

        public function set labelMargin(param1:String) : void
        {
            this._labelMargin = StringUtils.fillArrayByString(this._labelMargin, param1, int);
            callLater(this.changeLabelSize);
            return;
        }// end function

        public function get labelStroke() : String
        {
            return this._btnLabel.stroke;
        }// end function

        public function set labelStroke(param1:String) : void
        {
            this._btnLabel.stroke = param1;
            return;
        }// end function

        public function get labelSize() : Object
        {
            return this._btnLabel.size;
        }// end function

        public function set labelSize(param1:Object) : void
        {
            this._btnLabel.size = param1;
            callLater(this.changeLabelSize);
            return;
        }// end function

        public function get labelBold() : Object
        {
            return this._btnLabel.bold;
        }// end function

        public function set labelBold(param1:Object) : void
        {
            this._btnLabel.bold = param1;
            callLater(this.changeLabelSize);
            return;
        }// end function

        public function get letterSpacing() : Object
        {
            return this._btnLabel.letterSpacing;
        }// end function

        public function set letterSpacing(param1:Object) : void
        {
            this._btnLabel.letterSpacing = param1;
            callLater(this.changeLabelSize);
            return;
        }// end function

        public function get clickHandler() : Handler
        {
            return this._clickHandler;
        }// end function

        public function set clickHandler(param1:Handler) : void
        {
            this._clickHandler = param1;
            return;
        }// end function

        public function get btnLabel() : Label
        {
            return this._btnLabel;
        }// end function

        public function get sizeGrid() : String
        {
            if (this._bitmap.sizeGrid)
            {
                return this._bitmap.sizeGrid.join(",");
            }
            return null;
        }// end function

        public function set sizeGrid(param1:String) : void
        {
            this._bitmap.sizeGrid = StringUtils.fillArrayByString(Styles.defaultSizeGrid, param1);
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            super.width = param1;
            if (this._autoSize)
            {
                this._bitmap.width = param1;
            }
            callLater(this.changeLabelSize);
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            super.height = param1;
            if (this._autoSize)
            {
                this._bitmap.height = param1;
            }
            callLater(this.changeLabelSize);
            return;
        }// end function

        override public function set dataSource(param1:Object) : void
        {
            _dataSource = param1;
            if (param1 is Number || param1 is String)
            {
                this.label = String(param1);
            }
            else
            {
                super.dataSource = param1;
            }
            return;
        }// end function

        public function set customState(param1:String) : void
        {
            var _loc_2:* = param1.split(",");
            this._customState = {rollOver:int(_loc_2[0]), rollOut:int(_loc_2[1]), mouseDown:int(_loc_2[2]), mouseUp:int(_loc_2[3]), selected:int(_loc_2[4])};
            return;
        }// end function

    }
}
